/**
 * <p>Title: MaterialCodePropertyDaoImpl</p>
 * <p>Description: MaterialCodePropertyDao的实现类</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Chen Wei
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-23  Chen Wei
 *</pre>
*/
package cn.jhg.account.persist;


import javax.annotation.Resource;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@Repository 
public class MaterialCodePropertyDaoImpl implements MaterialCodePropertyDao{
	Logger logger=LoggerFactory.getLogger(MaterialCodePropertyDaoImpl.class);
	@Resource
	private MongoTemplate mongoTemplate;
	/**
	 * 添加原料编码属性
	 * @param code		MaterialCode(实体)
	 * @param propId		ObjectId
	 * @return	int
	 */
	@Override
	public int addCodeProperty(ObjectId propId, Document property) {
		DBObject object = new BasicDBObject();//设置需要插入到数据库的文档对象
		try{
		if(propId==null){
			logger.error("[ERR223195972] propId不存在,添加原料编码属性失败");
			return 223195972;//添加元数据失败
		}
			object.put("_id",propId);
			object.put("property",property);
			mongoTemplate.insert(object,"material_code_property");
			logger.info("[INF223195973] 添加原料编码属性成功");
			return 0;//添加原料编码属性成功
		}catch(Exception e){
			logger.error("[ERR223195992]添加原料编码属性失败",e.getMessage());
			return 223195992;//添加原料编码属性失败,数据库异常
		}
	}
	/**
	 * 更新原料编码属性
	 *  @param propId		ObjectId
	 * @param code		MaterialCode(实体)
	 */
	@Override
	public int updateCodeProperty(ObjectId propId, Document property) {
		try{
			Update update=new Update();
			update.set("property",property);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(propId)),
			update, "material_code_property");
			logger.info("[INF223196099] 更新原料编码属性成功");
			return 0;//更新原料编码属性成功
		}catch(Exception e){
			logger.error("[ERR223196006]更新原料编码属性失败",e.getMessage());
			return 223196006;//更新原料编码属性失败，异常错误代码
		}
	}
	/**
	 * 根据propId查询编码属性
	 * @param ObjectId propId
	 * @return		MaterialCodeProperty 实体
	 */
	@Override
	public Document getCodePropertyByPropId(ObjectId propId) {
		Document doc = new Document();
		try{	
			Document property = mongoTemplate.findById(propId,Document.class, "material_code_property");
			if(property==null){
				logger.error("[ERR224402041] 属性id不存在，通过属性id获取原料编码属性失败");
				doc.append("errorCode", 224402041);
			}else{
				logger.info("[INF223196021]根据propId查询编码属性成功");
				doc.append("errorCode", 0);
				doc.append("property", property);
			}
			return doc;
		}catch(Exception e){
			logger.error("[ERR223196060]根据propId查询编码属性失败",e.getMessage());
			doc.append("errorCode", 223196060);
			return doc;
		}
	}
	
	/**
	 * 删除原料编码属性
	 * @param ObjectId propId
	 * @return	errorCode
	 */
	@Override
	public int deleteCodeProperty(ObjectId propId) {
		try{	
			System.err.println(propId);
			mongoTemplate.remove(new Query(Criteria.where("_id").is(propId)), 
					Document.class, "material_code_property");
			logger.info("[INF223196071]根据propId删除原料编码属性成功");
			return 0;
		}catch(Exception e){
			logger.error("[ERR226994989]根据propId删除原料编码属性失败",e.getMessage());
			return 226994989; //errorCode
		}
	}
}
